#include "drag_widget.h"
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QDebug>
#include <QMimeData>
#include <QFile>


DragWidget::DragWidget(QWidget *parent)
: QTextEdit {parent}
{
	this->setPlaceholderText("请拖拽文件或输入内容！");
	// 默认提示文字
	this->setAcceptDrops(true);
	// 允许接受
	this->setEnabled(true);
}

void DragWidget::dragEnterEvent(QDragEnterEvent *event)
{
	qDebug() << "dragEnterEvent\n";
	event->acceptProposedAction();
}

void DragWidget::dragMoveEvent(QDragMoveEvent *event)
{
	qDebug() << "dragMoveEvent\n";
}

void DragWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
	qDebug() << "dragLeaveEvent\n";
}

void DragWidget::dropEvent(QDropEvent *event)
{
	// qDebug() << "dropEvent\n";
	QList<QUrl> file_ls = event->mimeData()->urls();
	if(file_ls.isEmpty()){
		this->setText(nullptr);
	}else {
		QString fileName = file_ls.first().toLocalFile();
		qDebug() << "Open File: " << fileName;
		QFile file(fileName);
		if(file.open(QIODevice::ReadOnly)){
			this->setPlainText(file.readAll());
		}
	}
}
